// 广告管理
app.controller('advertisementManage',['$scope','$modal','$http','$rootScope','$state', function($scope,$modal,$http,$rootScope,$state){
	//添加广告
	$scope.addAdvertisement=function(){
		var modalInstance = $modal.open({
		    templateUrl : 'addAdvertisement.html',
		    controller : 'addAdvertisement',
		    size : "sm",
		    backdrop:"static"
		});

		modalInstance.result.then(function() {
			$scope.getAdList();
		}, function() {
			$scope.getAdList();
		});
	}

	//编辑广告
	$scope.editAdvertisement=function(item){
		var modalInstance = $modal.open({
		    templateUrl : 'editAdvertisement.html',
		    controller : 'editAdvertisement',
		    size : "sm",
		    backdrop:"static"
		});

		modalInstance.result.then(function() {
			$scope.getAdList();
		}, function() {
			$scope.getAdList();
		});
		$rootScope.editInfo=item;
	}

	$scope.searchKey=[
		{
			"name":"全部",
			"id":""
		},
		{
			name:"链接地址",
			id:"1"
		},
		{
			name:"课程模块",
			id:"2"
		},
		{
			name:"试题模块",
			id:"3"
		}
	];
	$scope.nowSearchKey=$scope.searchKey[0];

	// 获取广告列表
	$scope.getAdList=function(){
		var data={
			page:1,
			page_size:15,
			position:2
		}
		if($scope.nowSearchKey.id){
			data.type=$scope.nowSearchKey.id;
		}
		$http({
		    url:$rootScope.baseUrl+"Ad/getAdList",
		    method:"post",
		    data:data
		}).then(function(res){
		    if(res.data.code==200){
		    	$scope.adList=res.data.data.list;
		    	if($scope.adList.length==0){
		    		$scope.noData=true;
		    	}else {
		    		$scope.noData=false;
		    	}

		    	// 显示分页
		    	var total=res.data.data.total;
		    	createPage(15,8,total);//每页15条数据，显示8个按钮
		    	function createPage(pageSize, buttons, total) {
		    	    $(".pagination").jBootstrapPage({
		    	        pageSize : pageSize,
		    	        total : total,
		    	        maxPageButton:buttons,
		    	        onPageClicked: function(obj, pageIndex) {
		    	            data.page=pageIndex+1;
		    	            $http({
		    	                method:"post",
		    	                url:$rootScope.baseUrl+'Ad/getAdList',
		    	                data:data
		    	            }).then(function(res){
		    	                if(res.data.code==200){
		    	                	$scope.adList=res.data.data.list;

		    	                	if($scope.adList.length==0 && Math.ceil(total/15)==1){
		    	                		$scope.noData=true;
		    	                	}else {
		    	                		$scope.noData=false;
		    	                	}
		    	                }
		    	            },function(error){
		    	                console.log(error);
		    	            });
		    	        }
		    	    });
		    	}
		    }else if(res.data.code==4002){
		    	$scope.adList=res.data.data.list;

		    	$scope.noData=true;

		    	// 显示分页
		    	var total=res.data.data.total;
		    	createPage(15,8,total);//每页15条数据，显示8个按钮
		    	function createPage(pageSize, buttons, total) {
		    	    $(".pagination").jBootstrapPage({
		    	        pageSize : pageSize,
		    	        total : total,
		    	        maxPageButton:buttons,
		    	        onPageClicked: function(obj, pageIndex) {
		    	            
		    	        }
		    	    });
		    	}
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});	
	}
	$scope.getAdList();

	
	$scope.selectSearchKey=function(item){
		$scope.nowSearchKey=item;
		$scope.getAdList();
	}

	$scope.deleteAdvertisement=function(item){
		swal({
		    title: "确定删除该广告？",
		    type: "warning",
		    showCancelButton: true,
		    confirmButtonColor: "#619ce1",
		    confirmButtonText: "确定",
		    closeOnConfirm: false
		},
		function() {
			$http({
			    url:$rootScope.baseUrl+"Ad/deleteAd",
			    method:"post",
			    data:{
			    	id:item.id
			    }
			}).then(function(res){
			    if(res.data.code==200){
			    	swal("删除成功", "", "success");
			    	$scope.getAdList();
			    }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
			},function(error){
			    console.log(error);
			});	
		});
	}
}]);

// 添加广告
app.controller('addAdvertisement', ['$scope','$modalInstance','$timeout','$rootScope', '$state',function($scope,$modalInstance,$timeout,$rootScope,$state){
	$scope.cancelAdd=function(){
		$modalInstance.close();
	}

	$scope.advertisementTypeList=[
		{
			name:"链接地址",
			id:"1"
		},
		{
			name:"课程模块",
			id:"2"
		},
		{
			name:"试题模块",
			id:"3"
		}
	];
	$scope.nowAdvertisementType=$scope.advertisementTypeList[0];

	$timeout(function(){
		var $showIcon=$("#showIcon");
		$("#advertisementIcon").on("change",function(event){
			var files=event.target.files;

			$.each(files,function(index,file){
				var reader = new FileReader();  
		        reader.onload = function (event) { 
		            $showIcon.attr("src",event.target.result);
		        }
		        reader.readAsDataURL(file); 
			});
		});
	},30);

	$scope.selectAdvertisementType=function(item){
		$scope.nowAdvertisementType=item;
	}

	$scope.save=function(){
		if($scope.value==undefined){
			swal("请填写ID或链接");
		}else {
			var formData = new FormData($("#addAdvertisement")[0]);

			$.ajax({
			    url: $rootScope.baseUrl +"Ad/addAd",
			    type: 'post',
			    data: formData,
			    xhrFields: {
	                withCredentials: true
	            },
			    async: false,
			    cache: false,
			    contentType: false,
			    processData: false,
			    global: false,
			    success: function(res) {
			        if(res.code==200){
			        	swal("添加成功", "", "success");
			        	$modalInstance.close();
			        }
			    },
			    error: function(error) {
			    	console.log(error);
			    }
			});
		}
	}
}]);

// 编辑广告
app.controller('editAdvertisement', ['$scope','$modalInstance','$timeout','$rootScope','$state', function($scope,$modalInstance,$timeout,$rootScope,$state){
	$scope.editInfo=$rootScope.editInfo;

	$scope.cancelAdd=function(){
		$modalInstance.close();
	}

	$scope.advertisementTypeList=[
		{
			name:"链接地址",
			id:"1"
		},
		{
			name:"课程模块",
			id:"2"
		},
		{
			name:"试题模块",
			id:"3"
		}
	];
	$scope.nowAdvertisementType=$scope.advertisementTypeList[$scope.editInfo.type-1];

	$timeout(function(){
		var $showIcon=$("#showIcon");
		$("#advertisementIcon").on("change",function(event){
			var files=event.target.files;

			$.each(files,function(index,file){
				var reader = new FileReader();  
		        reader.onload = function (event) { 
		            $showIcon.attr("src",event.target.result);
		        }
		        reader.readAsDataURL(file); 
			});
		});
	},30);

	$scope.selectAdvertisementType=function(item){
		$scope.nowAdvertisementType=item;
	}

	$scope.save=function(){
		if($scope.editInfo.value==undefined){
			swal("请填写ID或链接");
		}else {
			var formData = new FormData($("#editAdvertisement")[0]);

			$.ajax({
			    url: $rootScope.baseUrl +"Ad/editAd",
			    type: 'post',
			    data: formData,
			    xhrFields: {
	                withCredentials: true
	            },
			    async: false,
			    cache: false,
			    contentType: false,
			    processData: false,
			    global: false,
			    success: function(res) {
			        if(res.code==200){
			        	swal("编辑成功", "", "success");
			        	$modalInstance.close();
			        }
			    },
			    error: function(error) {
			    	console.log(error);
			    }
			});
		}
	}
}]);